layui.use(['form', 'layer', 'table'], function () {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		table = layui.table;

	//用户列表
	var tableIns = table.render({
		elem: '#cengciList',
		url: baseUrl + 'cengci/getCengci.php',
		skin: 'row',
		even: true, //开启隔行背景
		toolbar: true,
		defaultToolbar: ['filter'],
		cellMinWidth: 95,
		page: true,
		limits: [10, 15, 20, 25],
		limit: 10,
		id: "cengciListTable",
		cols: [
			[
				{
					field: 'id',
					title: 'ID',
				}, {
					field: 'name',
					title: '层次'
				},
				{
					title: '操作',
					width: 150,
					templet: '#cengciListBar',
					fixed: "right",
					align: "center"
				}
			]
		],
		done: function (res, curr, count) {// done 数据表格加载完成
		}
	});

	//添加层次
	function editCengci(edit) {
		var index = layui.layer.open({
			title: "层次编辑",
			type: 2,
			area: ['80%', '80%'],
			fixed: false, //不固定
			maxmin: true,
			shadeClose: true,
			content: "cengciEdit.html",
			success: function (layero, index) {
				const body = layui.layer.getChildFrame('body', index);
				if (edit) {//修改
					body.find(".addOrUpdate").val(1);
					body.find(".cengci_id").val(edit.id);
					body.find(".cengci_name").val(edit.name);
				} else {//添加
					body.find(".addOrUpdate").val(0);
				}
				setTimeout(function () {
					layui.layer.tips('点击此处返回层次列表', '.layui-layer-setwin .layui-layer-close', {
						tips: 3
					});
				}, 500)
			}
		})
		sessionStorage.setItem("index", index);
		//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
	}

	$(".addNews_btn").click(function () {
		editCengci();
	})
	//列表操作
	table.on('tool(cengciList)', function (obj) {
		const { id, name } = obj.data
		var layEvent = obj.event;
		if (layEvent === 'edit') { //编辑
			editCengci(obj.data);
		} else if (layEvent === 'del') { //删除
			layer.confirm('确定删除 ' + name + ' ？', {
				icon: 3,
				title: '删除确认'
			}, function (index) {
				$.ajax({
					url: baseUrl + "cengci/deleteCengci.php",
					data: {
						id
					},
					type: "POST",
					dataType: "text",
					success: function (data) {
						if (data == "1") {
							tableIns.reload();
							layer.close(index);
							layer.msg('删除 层次 ' + name + ' 成功！')
						} else {
							layer.msg('该 层次 下有数据，不能删除！')
						}
					}
				})
			});
		}
	});

	table.on('rowDouble(cengciList)', function (obj) {
		editCengci(obj.data);
	});
})
